package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import entity.Student_Info;

public class StudentDao {
	public void insert(Student_Info stu){
		String sql="insert into student_info values(?,?,?)";
		try {
			PreparedStatement pstm=DBUtil.getCon().prepareStatement(sql);
			
			pstm.setInt(1, stu.getId());
			pstm.setString(2, stu.getName());
			pstm.setString(3, stu.getGender());
			
			pstm.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
            e.printStackTrace();
		}
	}
	
	public void update(){
		
	}
	
	public void delete(Integer id){
		
	}
	
	public List<Student_Info> select(){
		List<Student_Info> list=new ArrayList<>();
		try {
			Statement stm=DBUtil.getCon().createStatement();
			
			ResultSet rs=stm.executeQuery("select * from student_info order by id");
			
			while(rs.next()){
				Student_Info stu=new Student_Info();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setGender(rs.getString("gender"));
				list.add(stu);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return null;
		}
	}
	
	public Student_Info select(Integer id){
		String sql="select * from student_info where id=?";
		try {
			PreparedStatement pstm=DBUtil.getCon().prepareStatement(sql);
			pstm.setInt(1, id);
			ResultSet rs=pstm.executeQuery();
			rs.next();
			
			Student_Info stu=new Student_Info();
			stu.setId(rs.getInt("id"));
			stu.setName(rs.getString("name"));
			stu.setGender(rs.getString("gender"));
			
			return stu;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			return null;
		}
	}
}
